package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AttributeTemplateAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api(tags = "04.属性模版管理模块")
@RestController
@RequestMapping("/attributeTemplates")
public class AttributeTemplateController {
    @Autowired
    private IAttributeTemplateService IAttributeTemplateService;

    public AttributeTemplateController(){
        log.debug("创建控制器对象：AttributeTemplateController");
    }

    @ApiOperation("添加属性模版")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@Valid AttributeTemplateAddNewDTO attributeTemplateAddNewDTO){
        IAttributeTemplateService.addNew(attributeTemplateAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除属性模版")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/deleteById")
    public JsonResult deleteById(@PathVariable("id") Long id){
        log.debug("开始处理【根据id删除属性模版】的请求，参数：{}",id);
        IAttributeTemplateService.deleteById(id);
        return JsonResult.ok();
    }


    @ApiOperation("修改属性模版详情")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult update(@PathVariable Long id, @Valid AttributeTemplateUpdateDTO attributeTemplateUpdateDTO){
        log.debug("开始处理【根据id修改属性模版基本信息】的请求，参数id：{}，新数据：{}",id ,attributeTemplateUpdateDTO);
        IAttributeTemplateService.updateInfoById(id, attributeTemplateUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("查询属性模版详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "属性模版id", required = true, dataType = "long")
    @GetMapping ("/{id:[0-9]+}")
    public JsonResult getStandardById(@PathVariable Long id){
        log.debug("开始处理【根据id查询属性模版详情】的请求，参数：{}",id);
        AttributeTemplateStandardVO attributeTemplateStandardVO = IAttributeTemplateService.getStandardById(id);
        return JsonResult.ok(attributeTemplateStandardVO);
    }


    @ApiOperation("属性模版列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult list(){
        log.debug("开始处理【属性模版列表】的请求，参数：无");
        List<AttributeTemplateListItemVO> list = IAttributeTemplateService.list();
        return JsonResult.ok(list);
    }

}
